<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/26
 * Time: 13:29
 */

namespace App\Utility\Pool;


use EasySwoole\Component\Pool\PoolObjectInterface;
use EasySwoole\Mysqli\Mysqli;

class MysqlObject extends Mysqli implements PoolObjectInterface
{
    /**
     * 释放对象是调用
     */
    function gc()
    {
        //重置为初始状态
        $this->resetDbStatus();
        //关闭数据库连接
        $this->getMysqlClient()->close();
    }

    /**
     * 回收对象是调用
     */
    function objectRestore()
    {
        //重置为初始状态
        $this->resetDbStatus();
    }

    /**
     * 每个链接使用之前 都会调用此方法 请返回 true/false
     * 返回false时PoolManager会回收改链接 并重新进入获取连接流程
     * @return bool
     */
    function beforeUse(): bool
    {
        //
        return $this->getMysqlClient()->connected;
    }
}